package com.quan.cms.web.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.base.common.base.web.support.BaseResult;
import com.quan.base.common.base.web.support.controller.BaseController;
import com.quan.cms.rpc.api.CmsChannelService;
import com.quan.cms.rpc.model.CmsChannel;
import com.quan.upms.annotation.RequiresPermissions;

@RestController
@RequestMapping(value = "/api/cms/channel")
public class CmsChannelController extends BaseController {

	@Autowired
	private CmsChannelService channelService;

	@RequiresPermissions(value="cms:channel:add", description="添加栏目")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody CmsChannel entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.channelService.insertSelective(entity);

		return BaseResult.ok();
	}

	@RequiresPermissions(value="cms:channel:delete", description="删除栏目")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.channelService.delete(id);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="cms:channel:update", description="修改栏目")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody CmsChannel entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		this.channelService.update(entity);

		return BaseResult.ok();
	}

	@RequiresPermissions(value="cms:channel:detail", description="查询栏目详情")
	@GetMapping(value = "/get/{id}")
	public Object list(@PathVariable Integer id) {
		return this.channelService.selectById(id);
	}
	
	@RequiresPermissions(value="cms:channel:list", description="查询栏目")
	@GetMapping(value = "/list")
	public Object list(CmsChannel entity) {
		Integer pid = 0 ;
		if(entity.getPid() != null) {
			pid = entity.getPid();
		}
		return this.channelService.selectChildren(pid);
	}

	@RequiresPermissions(value="cms:channel:tree", description="栏目树状")
	@GetMapping(value = "/tree")
	public Object tree(CmsChannel entity) {
		return this.channelService.tree(entity);
	}
	
	/**
	 * 获取节点的所有父节点
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/getParents")
	public Object getParents(Integer id) {
		return this.channelService.getParents(id);
	}
}
